package com.itheima.manager;

import com.itheima.common.R;
import com.itheima.domain.ProductAttribute;
import com.itheima.domain.ProductCategoryAttributeRelation;
import com.itheima.service.IProductAttributeService;
import com.itheima.service.IProductCategoryAttributeRelationService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class PmsProductAttributeManager {
    @DubboReference
    IProductAttributeService productAttributeService;
    @DubboReference
    IProductCategoryAttributeRelationService productCategoryAttributeRelationService;

    //根据商品分类的id获取商品属性及属性分类
    public R<List<Map<String, Long>>> findAttrIdAndAttrCateId(Long productCategoryId) {
        //首先查询商品分类商品属性关系表
        List<ProductCategoryAttributeRelation> productCategoryAttributeRelations= productCategoryAttributeRelationService.findByProductCategoryId(productCategoryId);

        //遍历集合，获得商品属性id,通过属性id查询商品属性对象
        //并从属性对象中获取属性id和属性分类id封装进map集合，再加入list集合
        List<Map<String,Long>> list=new ArrayList<>();
        productCategoryAttributeRelations.forEach(relation->{
            //获取属性id
            Long id = relation.getProductAttributeId();
            //查询属性对象
            ProductAttribute productAttribute=productAttributeService.findById(id);
            //将信息封装进map集合
            Map<String,Long> map=new HashMap<>();
            map.put("attributeCategoryId",productAttribute.getProductAttributeCategoryId());
            map.put("attributeId",productAttribute.getId());
            list.add(map);
        });
        return R.success(list);
    }

    //添加商品属性信息
    public R<String> create(ProductAttribute productAttribute) {
        productAttributeService.create(productAttribute);
        return R.success("添加成功");
    }

    //批量删除商品属性
    public R<String> deleteAttr(String ids) {
        //获取分类id集合
        List<Long> attrIds=new ArrayList<>();
        boolean b = ids.contains(",");
        if(b){
            String[] split = ids.split(",");
            for (String id : split) {
                attrIds.add(Long.valueOf(id));
            }
        }else{
            attrIds.add(Long.valueOf(ids));
        }
        productAttributeService.deleteAttr(attrIds);
        return R.success("删除成功");
    }

    //根据分类id查询属性列表或参数列表
    public R<PageBeanVo> findByCidAndPage(Long cid, Integer pageNum, Integer pageSize, Integer type) {
        PageBeanVo pageBeanVo=productAttributeService.findByCidAndPage(cid,pageNum,pageSize,type);
        return R.success(pageBeanVo);
    }

    //删除属性
    public R<String> updateAttr(ProductAttribute productAttribute) {
        productAttributeService.updateAttr(productAttribute);
        return R.success("修改成功");
    }

    //查询单个商品属性
    public R<ProductAttribute> findById(Long id) {
        ProductAttribute productAttribute=productAttributeService.findById(id);
        return R.success(productAttribute);
    }
}
